package ar.edu.uba.fi.jmit.monitorengine.backend.client.view;

import java.util.List;

import ar.edu.uba.fi.jmit.monitorengine.backend.client.controller.LoadedFilesController;
import ar.edu.uba.fi.jmit.monitorengine.backend.shared.dto.JMitFileDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;

public class LoadedFilesPanel extends VerticalPanel implements ValueChangeHandler<String> {
	
	private Grid filesGrid;
	
	private LoadedFilesController controller;
	
	public LoadedFilesPanel() {
		super();
		this.setSize("800px", "100%");
		this.add(new HTML("<h1>Configuration Files Loaded</h1>"));
		filesGrid = new Grid(0, 4);
		filesGrid.setTitle("Configuration Files Loaded");
		this.add(filesGrid);
		History.addValueChangeHandler(this);
	}

	@Override
	protected void onLoad() {
		GWT.log("Loading recived messages panel");
		super.onLoad();
	}
	
	public void showConfigurationFiles(List<JMitFileDTO> files) {
		filesGrid.clear();
		filesGrid.resizeRows(files.size() + 1);
		filesGrid.setBorderWidth(2);
		filesGrid.setSize("100%", "100%");
		int i = 1;
		this.addHeader(filesGrid);
		for(JMitFileDTO file:files) {
			this.addRow(i, filesGrid, file);
			i++;
		}
	}
	
	private void addHeader(Grid grid) {
		GWT.log("Adding Header");
		grid.setText(0, 0, "Name");
	}

	private void addRow(int rowNum, Grid grid, JMitFileDTO event) {
		grid.setText(rowNum, 0, event.getName());
		grid.setWidget(rowNum, 1, new Hyperlink("see", "confFile-see_" + event.getName()));
		grid.setWidget(rowNum, 2, new Hyperlink("remove", "confFile-remove_" + event.getName()));
		grid.setWidget(rowNum, 3, new Hyperlink("download", event.getPath()));
		
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		if(event.getValue().startsWith("confFile-see_")) {
			showConfigurationFile(event);
		}else if(event.getValue().startsWith("confFile-remove_")) {
			this.showConfirmation(event);
		}
		
	}

	private void showConfirmation(ValueChangeEvent<String> event) {
		String fileName = getFileName(event.getValue());
		ConfirmationDialog confirmation = new ConfirmationDialog("Are you sure you want to delete the file " + fileName + " and remove all it configuration?");
		confirmation.addConfirmHandler(new DeleteFileConfirmationHandler(fileName));
		confirmation.show();GWT.log("showing confirmation");
	}

	private String getFileName(String value) {
		return value.substring(value.indexOf("_") + 1);
	}

	private void showConfigurationFile(ValueChangeEvent<String> event) {
		String fileName = getFileName(event.getValue());
		GWT.log("Showing file " + fileName);
		controller.showFile(fileName);
	}

	public LoadedFilesController getController() {
		return controller;
	}

	public void setController(LoadedFilesController controller) {
		this.controller = controller;
	}
	
	private class DeleteFileConfirmationHandler implements ConfirmDialogHandler {
		
		private String fileName;
		
		public DeleteFileConfirmationHandler(String fileName) {
			this.fileName = fileName;
		}

		@Override
		public void onCancel() {
			History.newItem("");
		}

		@Override
		public void onConfirm() {
			controller.deleteFile(fileName);
			History.newItem("");
		}
		
	}
}
